import javax.swing.tree.TreeNode;

//计算布尔二叉树的值
//测试链接：https://leetcode.cn/problems/evaluate-boolean-binary-tree/
public class EvaluateTree {

      public class TreeNode {
       int val;
       TreeNode left;
       TreeNode right;
       TreeNode() {}
       TreeNode(int val) { this.val = val; }
       TreeNode(int val, TreeNode left, TreeNode right) {
           this.val = val;
           this.left = left;
           this.right = right;
        }
    }

    public boolean evaluateTree(TreeNode root) {
        if (root.left == null)
            return root.val == 0 ? false : true;
        boolean left = evaluateTree(root.left);
        boolean right = evaluateTree(root.right);
        return root.val == 2 ? left | right : left & right;
    }
}
